package com.citywy.service;

import com.citywy.entity.News;
import com.citywy.util.DBUtil;

import java.sql.SQLException;
import java.util.*;


public class NewsService {
    public static void main(String[] args) {
//        new NewsService().get(128760);
    }

    /**
     * 根据id获取要闻详情页
     * @param id
     * @return
     */
    public News get(int id) {
        News news = new News();
        List<Map<String,Object>> maps = null;
        Map<String, Object> whereMap = new HashMap<>();
        whereMap.put("id",id);
        try {
            maps = DBUtil.query("news",whereMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(maps!=null && maps.size()>0){
            Map map = maps.get(0);
            news.setId(Integer.parseInt(map.get("id").toString()));
            news.setTitle(map.get("title").toString());
            news.setLogo(map.get("logo").toString());
            news.setSummary(map.get("summary").toString());
            news.setContent(map.get("content").toString());
            news.setTag(map.get("tag").toString());
            news.setEmenu(Integer.parseInt(map.get("e_menu").toString()));
            news.setEaid(Integer.parseInt(map.get("e_aid").toString()));
            news.setEsave((Date)map.get("e_save"));
            news.setState((int)map.get("state"));
            news.setSource(map.get("source").toString());
            return news;
        }
        return null;
    }


    //获取共计多少条数据
    public int getRowCount(String condition){
        //计算一共多少条
        int rowcount=0;
        List<Map<String, Object>> maps = null;
        try {
            if(condition!=null){
                maps = DBUtil.executeQuery("select count(*) from news where "+condition,null);
            }else{
                maps = DBUtil.executeQuery("select count(*) from news", null);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            for (Map<String, Object> map:maps){
                rowcount=  Integer.parseInt(map.get("count(*)").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rowcount;
    }

    /**
     * 要闻分页
     * @param condition where条件
     * @param pageNow  当前第一页
     * @param pageSize 每页都少条
     * @param order 按什么列排序
     * @param sort  排序方式 asc desc
     * @return
     */
    public List<News> getNewsList(String condition, int pageNow, int pageSize, String order, String sort){
        List<News> list=new ArrayList<News>();
        //查询分页语句
        String sql=null;
        if(condition!=null){
            sql="select * from news where "+condition+" order by "+order+" "+sort+" limit "+(pageNow-1)*pageSize+","+pageSize;
        }else{
            sql="select * from news order by "+order+" "+sort+" limit "+(pageNow-1)*pageSize+","+pageSize;
        }
        List<Map<String, Object>> datas = null;
        try {
            datas = DBUtil.executeQuery(sql, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        News news=null;
        try {
            for (Map<String,Object> map:datas){
                news=new News();
                news.setId(Integer.parseInt(map.get("id").toString()));
                news.setTitle((String) map.get("title"));
                news.setLogo((String) map.get("logo"));
                news.setSummary((String)map.get("summary"));
                news.setEmenu((int)map.get("e_menu"));
                news.setEsave((Date) map.get("e_save"));
                news.setSource(map.get("source").toString());
                list.add(news);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 添加新闻
     * @param news
     * @return
     */
    public boolean addNews(News news){
        News news1 = new News();
        Map<String,Object> maps = new HashMap<String, Object>();
        maps.put("title",news.getTitle());
        maps.put("logo",news.getLogo());
        maps.put("summary",news.getSummary());
        maps.put("content",news.getContent());
        maps.put("tag",news.getTag());
        maps.put("e_menu",news.getEmenu());
        maps.put("e_aid",news.getEaid());
        maps.put("e_save",new Date());
        maps.put("e_chg",new Date());
        maps.put("state",1);
        maps.put("source",news.getSource());
        int num=0;
        try {
            num = DBUtil.insert("news",maps);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (num>0){
            return true;
        }else{
            return false;
        }
    }
}